﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using moose.irc;

namespace mooseirc.client.windowsforms {

    /// <summary>
    /// Form allows user to interface with IRC channel
    /// </summary>
    public sealed class ChannelForm : CommunicationChannelForm {

        #region [Local Attributes]

        /// <summary>
        /// Owning IRC Session
        /// </summary>
        private IrcSession m_session;

        /// <summary>
        /// Reference to the channel
        /// </summary>
        private IrcChannel m_channel;

        

        /// <summary>
        /// Reference to nicklist
        /// </summary>
        private controls.NickListControl nicklist;

        #endregion

        #region [Construction]

        /// <summary>
        /// Iniitalize Channel Form
        /// </summary>
        /// <param name="session">The IRC session that owns this channel</param>
        /// <param name="parent_form">parent mdi container form</param>
        /// <param name="channel">control to host</param>
        public ChannelForm(IrcSession session, MainForm parent_form, IrcChannel channel)
            : base(parent_form, new controls.ChannelControl(channel)) {
            m_channel = channel;
            m_session = session;
            input = ((controls.ChannelControl)m_HostedControl).input;
            output = ((controls.ChannelControl)m_HostedControl).output;
            nicklist = ((controls.ChannelControl)m_HostedControl).nicklist;
            input.UserResponse += new ResponseHandler(input_UserResponse);


            Text = m_channel.Name;


            return;
        }

        #endregion

        #region [Public Methods]

        /// <summary>
        /// Updates the user list
        /// </summary>
        public void update_users() {
            nicklist.update_userlist(m_channel);
            return;
        }

        #endregion

        #region [Event Handlers]

        /// <summary>
        /// Fires when user enters some input
        /// </summary>
        /// <param name="response">user input</param>
        void input_UserResponse(string response) {

            if (response.Length > 1) {
                if (response[0] == '/') {
                    //TODO: send a message to the session
                    m_session.send(response.Substring(1).Trim());
                } else {
                    privmsg(response);
                }
            } else {
                privmsg(response);
            }

        }

        #endregion

        #region [Overrides]

        /// <summary>
        /// Fires when user is closing the form
        /// </summary>
        /// <param name="e">Cancel Event Args</param>
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
            //TODO: enable an, are you sure you want to quit dialog, or at least make it an option
            //if they do not want to close, then use e.cancel

            m_session.send("PART " + m_channel.Name);

            base.OnClosing(e);
        }

        #endregion

        #region [Helper Methods]

        /// <summary>
        /// Send a private message to the channel
        /// </summary>
        /// <param name="message">message to send to the channel</param>
        private void privmsg(string message) {
            m_session.send("PRIVMSG " + m_channel.Name + " :" + message); 
            return;
        }

        #endregion

    }
}
